package com.liu.manager.service.impl;

import com.liu.manager.bean.ResponseResult;
import com.liu.manager.entity.Syspermission;
import com.liu.manager.mapper.SyspermissionMapper;
import com.liu.manager.service.SyspermissionService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SyspermissionSvcImpl implements SyspermissionService {

    @Resource
    private SyspermissionMapper syspermissionMapper;

    @Override
    public ResponseResult insertSyspermission(Syspermission syspermission) {
        if(!StringUtils.hasLength(syspermission.getPermissionName())){
            ResponseResult.ERROR("权限名称为空");
        }
        if(!StringUtils.hasLength(syspermission.getAction())){
            ResponseResult.ERROR("权限行为为空");
        }
        syspermission.setCreateTime(System.currentTimeMillis());
        syspermission.setUpdateTime(System.currentTimeMillis());
        syspermission.setStatus(1);
        long id = syspermissionMapper.insertPermission(syspermission);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(syspermission);
        return responseResult;
    }

    @Override
    public ResponseResult removeSyspermission(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        syspermissionMapper.deletePermission(id);
        return ResponseResult.OK();
    }

    @Override
    public ResponseResult getAllPermission() {

        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(syspermissionMapper.getAllPermissions());
        return responseResult;
    }

    @Override
    public ResponseResult findById(long id) {
        if(id <= 0){
            return ResponseResult.ERROR("id错误");
        }
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(100);
        responseResult.setMsg("保存成功");
        responseResult.setResult(syspermissionMapper.findById(id));
        return responseResult;
    }
}
